package com.poly.ureport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.poly.common.core.tools.utils.Func;
import com.poly.common.core.utils.AssertUtil;
import com.poly.common.core.utils.ConvertUtils;
import com.poly.common.core.web.page.Pagination;
import com.poly.ureport.domain.SysReport;
import com.poly.ureport.mapper.SysReportMapper;
import com.poly.ureport.pojo.dto.SysReportDTO;
import com.poly.ureport.pojo.query.SysReportQuery;
import com.poly.ureport.pojo.vo.SysReportVO;
import com.poly.ureport.service.ISysReportService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 报Service业务层处理
 *
 * @author poly
 * @date 2024-02-19
 */
@Service
public class SysReportServiceImpl extends ServiceImpl<SysReportMapper, SysReport> implements ISysReportService {
//
//    @Autowired
//    private RemoteUserService remoteUserService;
    /**
     * 获取查询对象
     *
     * @param query
     * @return
     */
    public LambdaQueryWrapper<SysReport> getWrapper(SysReportQuery query) {
        LambdaQueryWrapper<SysReport> wrapper = null;
        if (Func.isNotEmpty(query)) {
            wrapper = Wrappers.<SysReport>lambdaQuery();
            if (Func.isNotEmpty(query.getId())) {
                wrapper.eq(SysReport::getId, query.getId());
            }
            if (Func.isNotEmpty(query.getName())) {
                wrapper.like(SysReport::getName, query.getName());
            }
            if (Func.isNotEmpty(query.getContent())) {
                wrapper.eq(SysReport::getContent, query.getContent());
            }
            if (Func.isNotEmpty(query.getStatus())) {
                wrapper.eq(SysReport::getStatus, query.getStatus());
            }
        }
        return wrapper;
    }

    /**
     * 查询报
     *
     * @param id 报主键
     * @return 报
     */
    @Override
    public SysReport get(Long id) {
        return getById(id);
    }


    /**
     * 查询报
     *
     * @param id 报主键
     * @return 报
     */
    @Override
    public SysReportVO getVO(Long id) {
        return null;
    }
    /**
     * 查询报表分页
     *
     * @param query 报表
     * @return 报表
     */
    public Pagination<SysReportVO> queryPage(SysReportQuery query) {
        IPage<SysReport> page = baseMapper.selectPage(
                query.getPage(),
                getWrapper(query)
        );
        return Pagination.toPagination(page, SysReportVO.class);
    }

    /**
     * 查询报列表
     *
     * @param query 报
     * @return 报
     */
    @Override
    public List<SysReportVO> queryList(SysReportQuery query) {
        return ConvertUtils.convert(list(getWrapper(query)),SysReportVO.class);
    }

    /**
     * 新增报
     *
     * @param dto 报
     * @return 结果
     */
    @Override
    public boolean add(SysReportDTO dto) {
        SysReport entity = ConvertUtils.convert(dto, SysReport.class);
        return save(entity);
    }

    /**
     * 修改报
     *
     * @param dto 报
     * @return 结果
     */
    @Override
    public boolean update(SysReportDTO dto) {
        SysReport entity = ConvertUtils.convert(dto, SysReport.class);
        return updateById(entity);
    }

    /**
     * 批量删除报
     *
     * @param ids 需要删除的报主键
     * @return 结果
     */
    @Override
    public boolean deleteByIds(Long[] ids) {
        return removeBatchByIds(Arrays.asList(ids));
    }

    /**
     * 删除报信息
     *
     * @param id 报主键
     * @return 结果
     */
    @Override
    public boolean deleteById(Long id) {
        return removeById(id);
    }

    @Override
    public SysReport getByName(String name) {
        AssertUtil.isFalse(Func.isEmpty(name),"name is null");
        return getOne(Wrappers.<SysReport>lambdaQuery().eq(SysReport::getName, name));
    }

    @Override
    public void deleteByName(String name) {
        AssertUtil.isFalse(Func.isEmpty(name),"name is null");
        remove(Wrappers.<SysReport>lambdaQuery().eq(SysReport::getName, name));
    }
}
